package com.gidgabs.common.utils;



import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.Collection;
import java.util.List;

/**
 * 统一返回对象
 * @author tby
 */
@ApiModel(description = "统一返回对象")
public class ReturnJson<T> {

    @ApiModelProperty(notes = "状态码,0成功,非0失败",value = "状态码")
    private int status;
    @ApiModelProperty(notes = "响应消息内容",value = "响应消息")
    private String msg;

    //总记录数
    private long totalcount;
    //每页记录数
    private int pagesize;
    //总页数
    private int totalpage;
    //当前页数
    private int currpage;


    private Collection<T> list;

    private T obj;

    /**
     * 成功
     */
    public final static int Success = 0;

    /**
     * 失败
     */
    public final static int Fail = 100;
    /**
     * 未登录,或者没有权限
     */
    public final static int NOLOGIN = 401;
    /**
     *
     * @return
     */
    public final static int OTHER = 101;


    public int getStatus() {
        return status;
    }


    public String getMsg() {
        return msg;
    }


    public long getTotalcount() {
        return totalcount;
    }

    public void setTotalcount(int totalcount) {
        this.totalcount = totalcount;
    }

    public int getPagesize() {
        return pagesize;
    }

    public void setPagesize(int pagesize) {
        this.pagesize = pagesize;
    }

    public int getTotalpage() {
        return totalpage;
    }

    public void setTotalpage(int totalpage) {
        this.totalpage = totalpage;
    }

    public int getCurrpage() {
        return currpage;
    }

    public void setCurrpage(int currpage) {
        this.currpage = currpage;
    }

    public Collection<T> getList() {
        return list;
    }

    public ReturnJson<T> setList(Collection<T> list) {
        this.list = list;
        return this;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public T getObj() {
        return obj;
    }

    public  ReturnJson<T>  setObj(T obj) {
        this.obj = obj;
        return this;
    }

    /**
     * 返回列表
     * @param list            列表集合
     * @param totalCount    总数
     * @param currpage        页码
     */
    public ReturnJson<T> setList(List<T> list, long totalCount, int currpage) {
        this.list = list;
        this.totalcount = totalCount;
        this.currpage = currpage;
        return this;
    }


    public<T> ReturnJson() {
        setSuccess("操作成功");
    }


    /**
     * 返回正常值，表示处理成功<br>
     * 方 法 名：returnSuccess <br>
     * 创 建 人：tby <br>
     * 创建时间：2015年8月17日 下午3:01:58 <br>
     * 修 改 人： <br>
     * 修改日期： <br>
     * @param msg 自定义信息
     * @return
     * ReturnJson
     */
    public ReturnJson<T> setSuccess(String msg) {
        this.status = Success;
        this.msg = msg;
        return this;

    }
    public static <T> ReturnJson<T> success(String msg) {
        ReturnJson<T> returnJson=new ReturnJson();
        returnJson.status = Success;
        returnJson.msg = msg;
        return returnJson;
    }
    public static <T> ReturnJson<T> success(String msg,T t) {
        ReturnJson<T> returnJson=new ReturnJson();
        returnJson.status = Success;
        returnJson.msg = msg;
        returnJson.setObj(t);
        return returnJson;
    }
    public static <T> ReturnJson<T> success(String msg,List<T> t) {
        ReturnJson<T> returnJson=new ReturnJson();
        returnJson.status = Success;
        returnJson.msg = msg;
        returnJson.setList(t);
        return returnJson;
    }
    public static <T> ReturnJson<T> success(String msg,List<T> t,int totalcount) {
        ReturnJson<T> returnJson=new ReturnJson();
        returnJson.status = Success;
        returnJson.msg = msg;
        returnJson.totalcount=totalcount;
        returnJson.setList(t);
        return returnJson;
    }

    /***
     * 返回符合原分页工具包含的所有参数
     * @param msg
     * @param t
     * @param totalcount
     * @param pagesize
     * @param currpage
     * @param <T>
     * @return
     */
    public static <T> ReturnJson<T> success(String msg,List<T> t,long totalcount,int pagesize,int currpage) {
        ReturnJson<T> returnJson=new ReturnJson();
        returnJson.status = Success;
        returnJson.msg = msg;
        returnJson.totalcount=totalcount;
        returnJson.pagesize=pagesize;
        returnJson.currpage=currpage;
        returnJson.totalpage=(int)totalcount/pagesize;
        if (totalcount%pagesize>0){
            returnJson.totalpage++;
        }
        returnJson.setList(t);
        return returnJson;
    }
    public static <T> ReturnJson<T> fail(String msg) {
        ReturnJson<T> returnJson=new ReturnJson();
        returnJson.status = Fail;
        returnJson.msg = msg;
        return returnJson;
    }
    public static <T> ReturnJson<T> fail(String msg,T t) {
        ReturnJson<T> returnJson=new ReturnJson();
        returnJson.status = Fail;
        returnJson.msg = msg;
        returnJson.setObj(t);
        return returnJson;
    }

    public<T> ReturnJson(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    /**
     * 返回错误值，表示数据错误，业务处理失败<br>
     * 方 法 名：returnFail <br>
     * 创 建 人：tby <br>
     * 创建时间：2015年8月17日 下午3:02:17 <br>
     * 修 改 人： <br>
     * 修改日期： <br>
     * @param msg 失败的原因
     * @return
     * ReturnJson
     */
    public ReturnJson<T> setFail(String msg) {
        this.status = Fail;
        this.msg = msg;
        return this;
    }


    public ReturnJson<T> setOther(String msg) {

        this.status = OTHER;
        this.msg = msg;
        return this;
    }
    public ReturnJson<T> setOther(String msg,Integer status) {

        this.status = status;
        this.msg = msg;
        return this;
    }


}
